
# Tools
AR=ar
PP=cpp
AS=as
CC=gcc
CX=g++
LD=gcc
DB=gdb

# Tool Flags
PPFLAGS=-Isrc
ASFLAGS=
CCFLAGS=$(PPFLAGS) \
				-O2
CXFLAGS=$(PPFLAGS) \
				-O2
LDFLAGS=
LIBRARY=

# Project Files
TARGET1=libvec.a
OBJECT1=vec2.o \
				vec3.o \
				vec4.o \
				mat2.o \
				mat3.o \
				mat4.o \
				dvec2.o \
				dvec3.o \
				dvec4.o \
				dmat2.o \
				dmat3.o \
				dmat4.o \
				utility.o
DEPEND1=$(OBJECT1:.o=.d)

# Standard Targets
all: $(TARGET1)

run: $(TARGET1)
	@echo [EXEC] $<
	@./$(TARGET1)

debug: $(TARGET1)
	@echo [DEBUG] $<
	@$(DB) $<

clean:
	@echo [RM] $(TARGET1) $(OBJECT1) $(DEPEND1)
	-@rm $(TARGET1) $(OBJECT1) $(DEPEND1)

test:
	@make -C tests

# Project Targets
$(TARGET1): $(OBJECT1)
	@echo [AR] $@
	@$(AR) rc $@ $^

.SUFFIXES: .s .c .cpp .rc .l .y .d .o .exe .elf .hex .dll .def .a

# Dependency
.s.d:
	@echo [DEP] $@
	@$(PP) $(PPFLAGS) -MM -MT $(@:.d=.o) -o $@ $<

.c.d:
	@echo [DEP] $@
	@$(PP) $(PPFLAGS) -MM -MT $(@:.d=.o) -o $@ $<

.cpp.d:
	@echo [DEP] $@
	@$(PP) $(PPFLAGS) -MM -MT $(@:.d=.o) -o $@ $<

.rc.d:
	@echo [DEP] $<
	@$(PP) $(PPFLAGS) -MM -MT $(@:.d=.o) -o $@ $<

.l.d:
	@echo [DEP] $<
	@$(PP) $(PPFLAGS) -MM -MT $(@:.d=.o) -o $@ $<

.y.dep:
	@echo [DEP] $<
	@$(PP) $(PPFLAGS) -MM -MT $(@:.d=.o) -o $@ $<

# Compilation
.s.o:
	@echo [AS] $<
	@$(AS) $(ASFLAGS) -c -o $@ $<

.c.o:
	@echo [CC] $<
	@$(CC) $(CCFLAGS) -c -o $@ $<

.cpp.o:
	@echo [CXX] $<
	@$(CX) $(CXFLAGS) -c -o $@ $<

.rc.o:
	@echo [WINDRES] $<
	@windres -o $@ $<

.l.c:
	@echo [FLEX] $<
	@flex -o $@ $<

.y.c:
	@echo [BISON] $<
	@bison -d -o $@ $<

.l.o:
	@echo [FLEX] $<
	@flex -o $(@:.o=.c) $<
	@$(CC) $(CCFLAGS) -c -o $@ $(@:.o=.c)
	@rm -f $(@:.o=.c)

.y.o:
	@echo [BISON] $<
	@bison -d -o $(@:.o=.c) $<
	@$(CC) $(CCFLAGS) -c -o $@ $(@:.o=.c)
	@rm -f $(@:.o=.c)

# Converters
.elf.hex:
	@echo [OBJCOPY] $<
	@$(OC) -O ihex $< $@

.exe.hex:
	@echo [OBJCOPY] $<
	@objcopy -O ihex $< $@

.dll.def:
	@echo [GENDEF] $<
	@gendef - $< > $@

.def.a:
	@echo [DLLTOOL] $<
	@dlltool -d $< -l $@ -D $(@:.def=.dll)

# Load Dependency
-include $(DEPEND1)
